<?php

namespace App\Http\Controllers;



use App\Http\Requests\LevelRequest;
use App\Models\FsUser;
use App\Models\FsUserHasIm;
use App\Models\Level;
use App\Transformers\LevelTransformer;
use Illuminate\Http\Request;

class LevelController extends Controller
{
    /**
     * @api {post} /api/level 会员等级添加
     * @apiGroup Level
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} level  （必传参数）
     * @apiParam {String} colour  （必传参数）
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     */
    public function store(LevelRequest $levelRequest, Level $level){
        $level->create($levelRequest->all());
        return $this->response->created();
    }

    /**
     * @api {get} /api/level?page=1&limit=2 会员等级列表
     * @apiGroup Level
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               "id": 1,
     *               "level": "终极",
     *               "colour": "1111",
     *               "created_at": "2019-01-27 23:03:46",
     *               "updated_at": "2019-01-27 23:03:46"
     *               }
     *           ],
     *           "meta": {
     *               "pagination": {
     *                   "total": 2,
     *                   "count": 2,
     *                   "per_page": 10,
     *                   "current_page": 1,
     *                   "total_pages": 1,
     *                   "links": null
     *               }
     *           }
     *       }
     */
    public function index(Request $request,Level $level){
        $data = $level->paginate($request->get('limit',10));

        return $this->response->paginator($data, new LevelTransformer());
    }

    /**
     * @api {patch} /api/level/:id 会员等级修改
     * @apiGroup Level
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} id  level的id（必传参数）
     * @apiParam {String} level  （必传参数）
     * @apiParam {String} colour  （必传参数）
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     *       {
     *        "message": "修改成功"
     *       }
     */
    public function update(LevelRequest $request,Level $level){
        $level->update($request->all());

        return response()->json(['message' => '修改成功']);
    }

    /**
     * @api {delete} /api/level/:id 会员层级删除
     * @apiGroup Level
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id  会员层级ID（必传参数）
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 204 OK
     */
    public function destroy(Level $level){
        $level->delete();
        return $this->response->noContent();
    }

    /**
     * @api {post} /api/fsUser/level 会员关联表添加
     * @apiGroup  Level
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} fs_user_id  会员Id
     * @apiParam {int} level_id   等级id
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     */
    public function fsUserLevelAdd(Request $request){
        $data = $request->validate([
            'fs_user_id'=>'required|exists:mysql_branch.fs_user,id',
            'level_id' => 'required|exists:levels,id',
        ]);
        if($model = FsUser::find($data['fs_user_id'])->fsUserHasIm){
            //存在则修改
            $model->update(['level_id'=>$data['level_id']]);
        }else{
            //不存在则创建
            FsUserHasIm::create([
                'level_id'=>$data['level_id'],
                'fs_user_id'=>$data['fs_user_id']
            ]);
        }
        return $this->response->created();
    }
}
